<template>
  <nut-config-provider :theme="theme" :theme-vars="themeVars">
    <view :style="{backgroundColor:themeVars.pageBackgroundColor,padding: themeVars.pagePadding,height: '100vh'}">


      <!--      <view v-if="data.length > 0" v-for="item in data">
              <nut-swipe v-if="item.followType === 'ENTERPRISE'"  >
                <nut-cell>
                  <FollowShop :data="item.followObj" @click="Taro.navigateTo({url:'/pages/view/enterpriseIntro/index?id='+item.followObj.id})"  style="margin-top: 2px"/>
                </nut-cell>
                <template #right>
                  <nut-button shape="square" style="height: 100%" type="danger" @click="follow(item.followObj.id)">取消关注</nut-button>
                </template>
              </nut-swipe>
            </view>-->
      <Shop class="card" v-if="data.length > 0" v-for="item in data" :data="item.followObj"
            @click="Taro.navigateTo({url:'/pages/view/enterpriseIntro/index?id='+item.followObj.id})"
            style="margin-bottom: 5px"/>
      <nut-empty v-else :image="emptyUrl()" :image-size="100" description="暂无数据"/>
      <view style="display: flex;" v-if="loadMoreLoading">
        <IconFont name="loading"></IconFont>
      </view>

    </view>
  </nut-config-provider>
</template>

<script>
import {reactive, ref, toRaw} from 'vue';
import {IconFont} from '@nutui/icons-vue'
import useTheme from "../../../hooks/useTheme";
import Taro from "@tarojs/taro";
import {Phone1Reg} from "../../../utils/regUtils";
import FollowShop from "../../../components/followShop/index";
import {myCollectMyPage, myFollowMyPage} from "../../../api/login";
import {twPublishCollect, twPublishMyPage} from "../../../api/publish";
import {twProductCollect} from "../../../api/product";
import {twEnterpriseFollow} from "../../../api/enterprise";
import {emptyUrl} from "../../../utils/imgUtils";

// myCollectMyPage
const {theme, themeVars} = useTheme()

const data = ref([]);

const queryParam = ref({current: 1, size: 10, pages: 0})
const getData = () => {
  myFollowMyPage({...queryParam.value, sortField: 'createTime', sortOrder: "descend"}).then(res => {

    if (200 == res.code) {
      data.value = [...data.value, ...res.data.records]
      queryParam.value.pages = res.data.pages;
      console.log("queryParam", queryParam.value)
      console.log("res.data", res.data)
    }
  }).finally(() => {
    Taro.stopPullDownRefresh();
    loadMoreLoading.value = false

  })
}


const refresh = () => {
  queryParam.value.current = 1;
  queryParam.value.size = 10;
  data.value = []
  getData()
}

const loadMoreLoading = ref(false);
const loadMore = () => {
  if (queryParam.value.current >= queryParam.value.pages) {
    console.log("没有更多数据了")
    Taro.showToast({title: "没有更多数据了", icon: 'none', duration: 1500,});
    return
  }
  loadMoreLoading.value = true
  queryParam.value.current = queryParam.value.current + 1
  getData()
}


const productCollect = (id) => {
  twProductCollect({id: id}).then(res => {
    if (200 == res.code) {
      Taro.showToast({title: res.msg, icon: 'success', duration: 1500,});
      refresh()
    }
  }).finally(()=>{
    Taro.hideLoading();
  })
}


const publishCollect = (id) => {
  twPublishCollect({id: id}).then(res => {
    if (200 == res.code) {
      Taro.showToast({title: res.msg, icon: 'success', duration: 1500,});
      refresh()
    }
  }).finally(()=>{
    Taro.hideLoading();
  })
}

const follow = (id) => {
  twEnterpriseFollow({id: id}).then(res => {
    Taro.showToast({title: res.msg, icon: 'success', duration: 1500,});
    refresh()
  }).finally(()=>{
    Taro.hideLoading();
  })
}

export default {
  methods: {
    emptyUrl() {
      return emptyUrl
    }
  },
  components: {IconFont},

  // 可以使用所有的 Vue 生命周期方法
  mounted() {
  },

  onLoad() {
  },

  onReady() {
  },

  onShow() {
    refresh()
  },

  onHide() {
  },

  onPullDownRefresh() {
    console.log("onPullDownRefresh")
    refresh()
  },
  onReachBottom() {
    console.log("onReachBottom")
    loadMore()
  },


  setup() {


    return {
      theme,
      themeVars,
      data,
      getData,
      Taro,
      loadMoreLoading,
      productCollect,
      publishCollect,
      follow

    }
  }

}
</script>

<style>


</style>